package com.jair.adb;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import org.im4java.core.IMOperation;
import org.im4java.core.ImageCommand;


public class cutImage {
	public final static void cutImage(String srcImageFile, String result,  
	        int destWidth, int destHeight) {  
	    try {  
	        Iterator iterator = ImageIO.getImageReadersByFormatName("PNG");/*PNG,BMP*/     
	        ImageReader reader = (ImageReader)iterator.next();/*获取图片尺寸*/  
	        InputStream inputStream = new FileInputStream(srcImageFile);    
	        ImageInputStream iis = ImageIO.createImageInputStream(inputStream);     
	        reader.setInput(iis, true);     
	        ImageReadParam param = reader.getDefaultReadParam();     
	        Rectangle rectangle = new Rectangle(0,0, destWidth, destHeight);/*指定截取范围*/      
	        param.setSourceRegion(rectangle);     
	        BufferedImage bi = reader.read(0,param);   
	        ImageIO.write(bi, "JPEG", new File(result));  
	    } catch (Exception e) {  
	    	e.printStackTrace();
	        System.out.println("图片裁剪出现异常:"+e);
	    }  
	}
	
	/**
	 * 
	 * @Description: 裁剪图片
	 * @param @param srcFile源文件
	 * @param @param outFile输出文件
	 * @param @param x坐标
	 * @param @param y坐标
	 * @param @param width宽度
	 * @param @param height高度
	 * @param @return   
	 * @return boolean  
	 * @throws
	 * @author Gsz
	 * @date 2016-7-5 下午2:30:03
	 */
	public static boolean cutPic(String srcFile, String outFile, int x, int y,
			int width, int height) {
		FileInputStream is = null;
		ImageInputStream iis = null;
		try {
			// 如果源图片不存在
			if (!new File(srcFile).exists()) {
				return false;
			}

			// 读取图片文件
			is = new FileInputStream(srcFile);

			// 获取文件格式
			String ext = srcFile.substring(srcFile.lastIndexOf(".") + 1);

			// ImageReader声称能够解码指定格式
			Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(ext);
			ImageReader reader = it.next();

			// 获取图片流
			iis = ImageIO.createImageInputStream(is);
			//is.close();
			// 输入源中的图像将只按顺序读取
			reader.setInput(iis, true);

			// 描述如何对流进行解码
			ImageReadParam param = reader.getDefaultReadParam();

			// 图片裁剪区域
			Rectangle rect = new Rectangle(x, y, width, height);

			// 提供一个 BufferedImage，将其用作解码像素数据的目标
			param.setSourceRegion(rect);
			//is.close();
			// 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象
			BufferedImage bi = reader.read(0, param);

			// 保存新图片
			File tempOutFile = new File(outFile);
			if (!tempOutFile.exists()) {
				tempOutFile.mkdirs();
			}
			ImageIO.write(bi, ext, new File(outFile));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (is != null) {
					is.close();
				}
				if (iis != null) {
					iis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}
	}
	
	// 图片转化成base64字符串
		public String GetImageStr(String filePathMsg) {
			// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
			InputStream in = null;
			byte[] data = null;
			// 读取图片字节数组
			try {
				in = new FileInputStream(filePathMsg);
				data = new byte[in.available()];
				in.read(data);
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		
			return new sun.misc.BASE64Encoder().encode(data);// 返回Base64编码过的字节数组字符串
		}

		// base64字符串转化成图片
		public boolean GenerateImage(String imgStr,String imgPath,String rootPath) { 
			// 对字节数组字符串进行Base64解码并生成图片
			if (imgStr == null) // 图像数据为空
				return false;
			sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();  
			try {
				// Base64解码
				byte[] b = decoder.decodeBuffer(imgStr);
				for (int i = 0; i < b.length; ++i) {
					if (b[i] < 0) {// 调整异常数据
						b[i] += 256;
					}
				}
				// 生成jpeg图片
				String imgFilePath = rootPath + imgPath;// 新生成的图片
				OutputStream out = new FileOutputStream(imgFilePath);
				out.write(b);
				out.flush();
				out.close();
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		
	public static void main(String[] args){
		String file1 = "D:\\test21515729916146.png";
		String file1_1 = "D:\\test31515733913205.png";
		String file2 = "D:\\test2151572962021444.png";
		cutPic(file1,file2,75,286,985,888);
	}
	
	
}
